//
//  XMN_CollectionViewController.m
//  B段项目
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "XMN_CollectionViewController.h"
#import "GZ_CoreDataManager.h"
#import "GZ_RadioModel.h"
#import "GZRadioTableViewCell.h"
#import "GZ_RadioDetailsVC.h"
#import "GZ_CollectionVIewCell.h"
#import "RadioModel.h"
#import "GZ_RadioViewController.h"
@interface XMN_CollectionViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSArray *dataSourceArray;
@property(nonatomic, strong)GZ_CoreDataManager *manager;

@property(nonatomic, strong)NSMutableArray *StringArray;
@end

@implementation XMN_CollectionViewController

- (void)viewWillAppear:(BOOL)animated
{
    _dataSourceArray = [NSArray array];
    _manager = [GZ_CoreDataManager shareManager];
    //创建查询请求对象
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"RadioModel"];
    
    //        NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
    //        [fetchRequest setPredicate:predicate];
    
    _dataSourceArray = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    
    _StringArray = [NSMutableArray array];
    for (RadioModel *model in _dataSourceArray) {
        NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithData:model.userinfo];
        [_StringArray addObject:dic];
    }
    
    if (_dataSourceArray.count == 0) {
        
        DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"没有收藏" contentText:@"快去收藏您喜欢的电台吧" leftButtonTitle:nil rightButtonTitle:@"好的"];
        [alert show];
        alert.rightBlock = ^(){
            GZ_RadioViewController *ra = [[GZ_RadioViewController alloc] init];
            [self.navigationController pushViewController:ra animated:YES];
        };
    }
    [_tableView reloadData];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"电台收藏";
    // Do any additional setup after loading the view.
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView NightWithType:UIViewColorTypeNormal];
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}

//cell赋值------------------------------------------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"GZ";
    GZ_CollectionVIewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[GZ_CollectionVIewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.radioModel = _dataSourceArray[indexPath.row];
    cell.authorLabel.text = [NSString stringWithFormat:@"by:%@", [_StringArray[indexPath.row] valueForKey:@"uname"]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GZ_RadioDetailsVC *gd = [[GZ_RadioDetailsVC alloc] init];
    gd.radioModel = _dataSourceArray[indexPath.row];
        [self.navigationController pushViewController:gd animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120 * FitHeight;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
